import Vue from 'vue'
import Vuex from 'vuex'
import * as api from "../tools/axios";
//import axios from "../tools/axios"

Vue.use(Vuex);


//export default store;
export function createStore(){
	const store=new Vuex.Store({
		state:{list:[],number:0,user:{}},
		actions:{
			set_list({commit,state},data){
				//console.log('action set_list')
				//commit('set_list',data);
				// return axios.get('/list').then(d=>{
				// 	//console.log('actions  set_list',d);
				// 	commit('set_list',d.data)
				// });
				return api.getlist().then(d=>{
					//console.log('actions  set_list',d);
					commit('set_list',d.data)
				});
			},add_number({commit,state}){
				//console.log('action add_number')
				commit('add_number',state.number+1)
			},set_user({commit,state},data){
				if(data){
					commit('set_user',data);
					return ;
				}
				return api.getuser().then(d=>{
					console.log('actions  set_user',d);
					commit('set_user',d.data)
					return d;
				})
			},destroy_user({commit,state}){
				return api._axios.post('/user/signout').then(d=>{
					commit('set_user',{})
				})
			}
		},mutations:{
			set_list(state,data){
				state.list=data;
				//console.log(state.list)
			},add_number(state,data){
				state.number=data;
			},set_user(state,data){
				state.user=data;
			}
		},getters:{
			getRoute(state){
				return state.route ||{};
			},getuser(state){
				return state.user || {};
			},getToken(state){
				return state.user &&  state.user.token? state.user.token:false;
			}	
		}
	})
	return store;
}